﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace LUC.Services
{
	public class ColorTextBox : UserControl
	{
		private Label lb;
		private ColorDialog cd;

		public event EventHandler CurrentColorChanged;
		public event EventHandler CurrentIntColorChanged;

		public ColorTextBox()
		{
			InitializeComponent();
			cd.FullOpen = true;
			SetColor(cd.Color);
		}

		private void InitializeComponent()
		{
			this.cd = new System.Windows.Forms.ColorDialog();
			this.lb = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lb
			// 
			this.lb.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lb.Location = new System.Drawing.Point(0, 0);
			this.lb.Margin = new System.Windows.Forms.Padding(0);
			this.lb.Name = "lb";
			this.lb.Size = new System.Drawing.Size(63, 20);
			this.lb.TabIndex = 0;
			this.lb.Text = "label1";
			this.lb.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.lb.Click += new System.EventHandler(this.lb_Click);
			// 
			// ColorTextBox
			// 
			this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
			this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.Controls.Add(this.lb);
			this.Cursor = System.Windows.Forms.Cursors.Hand;
			this.MaximumSize = new System.Drawing.Size(500, 30);
			this.MinimumSize = new System.Drawing.Size(65, 22);
			this.Name = "ColorTextBox";
			this.Size = new System.Drawing.Size(63, 20);
			this.Click += new System.EventHandler(this.lb_Click);
			this.ResumeLayout(false);

		}

		public Color CurrentColor
		{
			get
			{
				return cd.Color;
			}
			set
			{
				cd.Color = value;
				SetColor(value);
				//OnCurrentColorChanged(this, EventArgs.Empty);
			}
		}
		public int CurrentIntColor
		{
			get { return ColorTranslator.ToWin32(CurrentColor);}
 			set
			{
				CurrentColor = ColorTranslator.FromWin32(value);
				OnCurrentIntColorChanged(this, EventArgs.Empty);
			}
		}

		private void SetColor(Color c)
		{
			this.BackColor = c;
			lb.BackColor = c;
			lb.ForeColor = ColorTranslator.FromWin32((2<<24)-1 - ColorTranslator.ToWin32(c));
			lb.Text = Color2String(c);
		}
		
		private void lb_Click(object sender, EventArgs e)
		{
			var c = cd.Color;
			if (cd.ShowDialog() != DialogResult.OK)
				return;
			SetColor(cd.Color);
			
			if (CurrentColorChanged != null)
			    CurrentColorChanged(this, EventArgs.Empty);
			if (CurrentIntColorChanged != null)
			    CurrentIntColorChanged(this, EventArgs.Empty);
		}

		string b2s(byte x)
		{
			string s = Convert.ToString(x, 16).ToUpper();
			if (s.Length == 1)
				s = "0" + s;
			return s;
		}
		string Color2String(Color c)
		{
			return b2s(c.R) + " " + b2s(c.G) + " " + b2s(c.B);
		}

		//protected virtual void OnCurrentColorChanged(object objSrc, EventArgs args)
		//{
		//    if (CurrentColorChanged != null)
		//        CurrentColorChanged(objSrc, args);
		//}
		public virtual void OnCurrentIntColorChanged(object objSrc, EventArgs args)
		{
			if (CurrentIntColorChanged != null)
				CurrentIntColorChanged(objSrc, args);
		}
	}
}
